/**
 * <p>Title: ServiceZM9M.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-12-17 上午08:08:00  张志芳  Create
 *</pre>
 */
package com.baosight.sczx.zm.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlockMeta;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;

/**
 * 生产情况
 * <p>Title: ServiceZM9M.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-12-17 上午08:08:00  张志芳  Create
 *</pre>
 */
public class ServiceZM9M extends ServiceEPBase {
	
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZM9M</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-12-17上午08:33:50 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		return query(inInfo);
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZM9M</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-12-17上午08:34:00 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
		EiInfo outInfo = new EiInfo();
		Map<String, ArrayList<Map>> wornums = getWornum();//获取所有的班组信息
		
		int n=0;
		for(Map.Entry<String, ArrayList<Map>> entry : wornums.entrySet()){
			n++;
			//采煤
			outInfo.addBlock("mineResult"+n).addBlockMeta(getMeta(entry.getValue()));
			List<Map> mineSum = getData("ZM9M.queryMine", entry.getKey(), entry.getValue().size());//查询当日的总数据
			List<Map> mineDetail = getDataDetail("ZM9M.queryMineDetail", entry.getKey());//查询各班的数据
			
			for(int i=0; i<mineSum.size(); i++){
				Map row = makeDetail(mineSum.get(i).get("fdDepartmentId").toString(),mineDetail,entry.getValue());//将各班的实际数据组织成一条数据
				row.put("fdDepartment", mineSum.get(i).get("fdDepartment"));
				row.put("actualSum", mineSum.get(i).get("fdSum"));
				row.put("planSum", mineSum.get(i).get("fdPlanSum"));
				String wornumPlanSum = String.format("%.2f", mineSum.get(i).get("fdWornumPlanSum"));
				row = putWornumPlan(entry.getValue(),wornumPlanSum,row);//将计划平均值放入各班中
				
				outInfo.getBlock("mineResult"+n).addRow(row);
			}
			
			//掘进
			outInfo.addBlock("grubResult"+n).addBlockMeta(getMeta(entry.getValue()));
			List<Map> grubSum = getData("ZM9M.queryGrub", entry.getKey(), entry.getValue().size());
			List<Map> grubDetail = getDataDetail("ZM9M.queryGrubDetail", entry.getKey());
			
			for(int i=0; i<grubSum.size(); i++){
				Map row = makeDetail(grubSum.get(i).get("fdDepartmentId").toString(),grubDetail,entry.getValue());
				row.put("fdDepartment", grubSum.get(i).get("fdDepartment"));
				row.put("actualSum", grubSum.get(i).get("fdSum"));
				row.put("planSum", grubSum.get(i).get("fdPlanSum"));
				String wornumPlanSum = String.format("%.2f", grubSum.get(i).get("fdWornumPlanSum"));
				row = putWornumPlan(entry.getValue(),wornumPlanSum,row);
				
				outInfo.getBlock("grubResult"+n).addRow(row);
			}
		}
		
		return outInfo;
	}
	
	/**
	 * 将计划的平均值分配到各班中
	 * <p>Function: putWornumPlan</p>
	 * <p>Description: </p>
	 * <p>return value:Map</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-12-18上午07:58:02 张志芳
	 * </p>
	 *
	 */
	private Map putWornumPlan(ArrayList<Map> map, String value, Map row)
	{
		int m=0;
		for(Map entry : map){
			m++;
			row.put("plan"+m, value);
		}
		return row;
	}
	
	/**
	 * 根据部门，将查询出来的各班实际数据，组织成一条数据
	 * <p>Function: makeDetail</p>
	 * <p>Description: </p>
	 * <p>return value:Map</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-12-18上午07:58:52 张志芳
	 * </p>
	 *
	 */
	private Map makeDetail(String detId, List<Map> list, ArrayList<Map> map)
	{
		Map row = new HashMap();
		for(int j=0; j<list.size(); j++){
			if(detId.equals(list.get(j).get("fdReportDepartmentId"))){
				int m=0;
				for(Map entry : map){
					m++;
					if(entry.get("fdKeyId").toString().equals(list.get(j).get("fdWorknumId"))){
						row.put("actual"+m, list.get(j).get("fdSum"));
						break;
					}
				}
			}
		}
		return row;
	}
	
	/**
	 * 获取当日的计划和实际总计值
	 * <p>Function: getData</p>
	 * <p>Description: </p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-12-18上午08:00:13 张志芳
	 * </p>
	 *
	 */
	private List<Map> getData(String sql, String wornum, int num)
	{
		Map map = new HashMap();
		map.put("fdDate", StaticTools.getDate());
		map.put("fdWornum", wornum);
		map.put("fdYear", StaticTools.getYear());
		map.put("fdMonth", Integer.parseInt(StaticTools.getMonth()));
		map.put("num", num);
		return dao.query(sql, map);
	}
	
	/**
	 * 获取各班的实际数值
	 * <p>Function: getDataDetail</p>
	 * <p>Description: </p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-12-18上午08:01:07 张志芳
	 * </p>
	 *
	 */
	private List<Map> getDataDetail(String sql, String wornum)
	{
		Map map = new HashMap();
		map.put("fdDate", StaticTools.getDate());
		map.put("fdWornum", wornum);
		return dao.query(sql, map);
	}
	
	/**
	 * 获取班组信息
	 * <p>Function: getWornum</p>
	 * <p>Description: 将班组安装不同的班制(三班制，四班制)分类，记录各班制中的班次的ID和名字</p>
	 * <p>return value:Map<String,Map<String,String>></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-12-18上午06:36:15 张志芳
	 * </p>
	 *
	 */
	private Map<String, ArrayList<Map>> getWornum()
	{
		Map<String, ArrayList<Map>> result = new HashMap<String, ArrayList<Map>>();

		List<Map> list = dao.query("ZM9M.queryWornum", null);
		for(int i=0; i<list.size(); i++){
			String wornum = list.get(i).get("fdScheduleClassify").toString();
			if(!result.containsKey(wornum)){
				ArrayList<Map> map = new ArrayList<Map>();
				map.add(list.get(i));
				//map.put(list.get(i).get("fdKeyId").toString(), list.get(i).get("fdScheduleNum").toString());
				result.put(wornum, map);
			}
			else
			{
				ArrayList<Map> map = result.get(wornum);
				map.add(list.get(i));
				//map.put(list.get(i).get("fdKeyId").toString(), list.get(i).get("fdScheduleNum").toString());
			}
		}
		return result;
	}
	
	/**
	 * 构造列
	 * <p>Function: getMeta</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlockMeta</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-12-12上午05:46:20 张志芳
	 * </p>
	 *
	 */
	private EiBlockMeta getMeta(ArrayList<Map> wornum)
	{			
		EiBlockMeta eiMetadata = new EiBlockMeta();
		EiColumn eiColumn = new EiColumn("fdDepartment");
		eiColumn.setFieldLength(100);	
		eiColumn.setDescName("施工部门");
		eiMetadata.addMeta(eiColumn);		
		
		int i = 1;
		for(Map entry : wornum)
		{
			eiColumn = new EiColumn("plan" + (i));
			eiColumn.setFieldLength(15);	
			eiColumn.setDescName(entry.get("fdScheduleNum").toString().split("/")[0] + "计划");
			eiMetadata.addMeta(eiColumn);
			
			eiColumn = new EiColumn("actual" + (i));
			eiColumn.setFieldLength(15);	
			eiColumn.setDescName(entry.get("fdScheduleNum").toString().split("/")[0] + "实际");
			eiMetadata.addMeta(eiColumn);
			i++;
		}
		
		eiColumn = new EiColumn("planSum");
		eiColumn.setFieldLength(8);	
		eiColumn.setDescName("计划总计");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("actualSum");
		eiColumn.setFieldLength(8);	
		eiColumn.setDescName("实际总计");
		eiMetadata.addMeta(eiColumn);
		
		return eiMetadata;
	}
}
